package com.org.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.org.pojo.Community;
import com.org.common.PageBean;
import com.org.common.Result;
import com.org.pojo.CommunityActivity;
import com.org.pojo.NormalUser;
import com.org.pojo.Serve;
import com.org.service.CommunityService;
import com.org.service.ICommunityActivityService;
import com.org.service.NormalUserService;
import com.org.service.ServeService;
import com.org.utils.ThreadLocalUtil;
import org.jooq.impl.QOM;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * ClassName: CommunityController
 * Package: com.org.controller
 * Description:
 *
 * @Author Origin
 * @Create 2024/2/2 10:11
 * @Version 1.0
 */
@RestController
@RequestMapping("/community")
@CrossOrigin//支持跨域
public class CommunityController {

    @Autowired
    private CommunityService communityService;

    @Autowired
    private NormalUserService normalUserService;

    @Autowired
    private ICommunityActivityService communityActivityService;

    @Autowired
    private ServeService serveService;

    @GetMapping("/list")
    public Result<PageBean<Community>> list(
            Integer pageNum,    //第几页
            Integer pageSize,   //每页几个
            @RequestParam(required = false) String province, //可以不携带
            @RequestParam(required = false) String city,
            @RequestParam(required = false) String districtOrCountry,
            @RequestParam(required = false) String name,
            @RequestParam(required = false) String state
    ){
        PageBean<Community> pb = communityService.list(pageNum, pageSize, province, city, districtOrCountry, name, state);
        return Result.success(pb);
    }

    @PostMapping("/add")
    public Result add(@RequestBody @Validated Community community){
        //判断是否存在相同社区
        PageBean<Community> pb = communityService.list(1, 1, community.getProvince(),
                                 community.getCity(), community.getDistrictOrCountry(), community.getName(), null);
        if(pb.getTotal() != 0){
            return Result.error("已存在该社区");
        }
        communityService.add(community);
        return Result.success("操作成功，正在等待管理员是审核...");
    }

    @GetMapping("/detail")
    public Result<Community> detail(Integer id){
        Community c = communityService.getById(id);
        return Result.success(c);
    }

    @PostMapping("/update")
    public Result update(@RequestBody Community community){

        communityService.update(community);
        return Result.success();
    }

    @PostMapping("/updateState")
    public Result updateState(@RequestBody Map<String, Object> map){
        Integer communityId = (Integer)map.get("communityId");
        String state = (String) map.get("state");
        System.out.println(communityId);
        System.out.println(state);
        communityService.updateState(state, communityId);
        return Result.success();
    }

    @GetMapping("/delete")
    public Result delete(Integer id){
        communityService.delete(id);
        return Result.success();
    }

    /**
     * 用户加入社区
     * {
     * "communityId": 5
     * }
     */
    @PostMapping("/userJoin")
    public Result userJoinCommunity(@RequestBody  Map<String, Integer> map) {
        // 获取用户id与社区号
        Integer communityId = map.get("communityId");
        Map<String, Object> params = ThreadLocalUtil.get();
        Integer userId = (Integer) params.get("id");
        // 更新用户所在社区
        NormalUser user = normalUserService.getById(userId);
        user.setCommunityId(communityId);
        normalUserService.update(user);
        return Result.success();
    }

    /**
     * 用户退出社区
     * {
     * "communityId": 5
     * }
     */
    @PostMapping("/userExit")
    public Result exitJoinCommunity() {
        // 获取用户id
        Map<String, Object> params = ThreadLocalUtil.get();
        Integer userId = (Integer) params.get("id");
        // 更新用户所在社区
        NormalUser user = normalUserService.getById(userId);
        user.setCommunityId(null);
        normalUserService.update(user);
        return Result.success(user);
    }

    /**
     * 获取社区的用户人数、活动数量和服务数量
     * 返回社区的以下信息值
     * {
     *      "peopleNum": 5,   // 加入人数
     *      "activityNum": 1,   // 活动数目
     *      "serviceNum": 1  // 服务数量
     *  }
     */
    @GetMapping("/communitySummary")
    public Result<Map<String, String>> communitySummary(Integer communityId){
        Map<String, String> result = new HashMap<>();
        // 查询社区参与人数
        int peopleNum = normalUserService.list(new LambdaQueryWrapper<NormalUser>().eq(NormalUser::getCommunityId, communityId)).size();
        result.put("peopleNum", String.valueOf(peopleNum));
        // 查询社区活动数量
        int activityNum = communityActivityService.list(new LambdaQueryWrapper<CommunityActivity>().eq(CommunityActivity::getCommunityId, communityId)).size();
        result.put("activityNum", String.valueOf(activityNum));
        // 查询服务数量
        int serviceNum = serveService.list(new LambdaQueryWrapper<Serve>().eq(Serve::getCommunityId, communityId)).size();
        result.put("serviceNum", String.valueOf(serviceNum));
        return Result.success(result);
    }

}
